﻿CREATE PROCEDURE [ServiceEngine].[ListReferencedBySchemas] 
	@SchemaID uniqueidentifier
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	SELECT DISTINCT 
		N.EntityID,
		N.EntityName
	FROM
		ServiceEngine.[Schemas] S JOIN ServiceEngine.SchemaObjects SO ON S.SchemaID = SO.SchemaID 
		JOIN ServiceEngine.SchemaObjectDependencies SOD ON SO.SchemaObjectID = SOD.ParentObjectID
		JOIN ServiceEngine.SchemaObjects SO2 ON SO2.SchemaObjectID = SOD.SchemaObjectID
		JOIN ServiceEngine.[Schemas] S2 ON S2.SchemaID = SO2.SchemaID
		JOIN ServiceEngine.[Names] N ON S.SchemaID = N.EntityID
	WHERE
		S2.SchemaID = @SchemaID
END